package com.newbye.files
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.controls.Alert;

	public class PanelFile implements IPanelFile
	{
		private var _className:String;
		private var _parentClass:String;
		private var _path:String;
		public function PanelFile(className:String, parentClass:String, path:String)
		{
			this._className = className;
			_parentClass = parentClass;
			this._path = path;
		}
		
		public function get className():String
		{
			return _className;
		}
		
		public function create():Boolean
		{
			return createFile();
		}
		
		public function save():Boolean
		{
			return true;
		}
		
		private function createFile():Boolean
		{
			var rb:Boolean = false;
			if(this._className && this._path)
			{
				var fname:String = this._path + "/" + this._className + ".as";
				var f:File = new File(fname);
				if(!f.exists)
				{
					var fs:FileStream = new FileStream();
					try
					{
						fs.open(f, FileMode.WRITE);
						var cstr:String = PanelTemplateFactory.createByTemplate1(_className, _parentClass);
						fs.writeMultiByte(cstr, File.systemCharset);
						rb = true;
						fs.close();
					}
					catch(e:Error)
					{
						Alert.show(e.message);
					}
					
				}
				else
				{
					Alert.show("File'" + fname + "' allready Exists");
				}
			}
			return rb;
		}
	}
}